package com.zc.chapter42.controller;

import javafx.beans.binding.ObjectExpression;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Modifier;
import java.util.Calendar;

@Component
@Aspect
public class LogAspect {

    private final Logger logger =  LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.zc.chapter42.controller.HelloController.*(..))")
    public void cut(){}


    //第一个知识点


    @Before("cut()")
    public void doBefore(JoinPoint joinPoint){

        //第一个知识
        Signature signature = joinPoint.getSignature();

        String methodName = signature.getName();
        System.out.println("目标方法名称：" +methodName );

        String className = signature.getDeclaringType().getSimpleName();
        System.out.println("简单类名："+className);

        String fullClassName = signature.getDeclaringType().getName();
        System.out.println("类名："+fullClassName);

        String modifiersName = Modifier.toString(joinPoint.getSignature().getModifiers());
        System.out.println("方法类型："+modifiersName);



        //第二个
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attributes.getRequest();


        System.out.println("Web请求URI："+request.getRequestURI());

        System.out.println("Web请求URL:"+request.getRequestURL());

        System.out.println("Web请求Method:"+request.getMethod());


        System.out.println("调用开始时间: " + Calendar.getInstance().getTimeInMillis());
    }

    //调用后执行
    @After("cut()")
    public void doAfter(){
        System.out.println("调用结束时间: " + Calendar.getInstance().getTimeInMillis());
    }

    @AfterReturning(returning = "obj" ,pointcut = "cut()")
    public void doAfterReturning(Object obj){

        System.out.println("返回值的内容:"+obj.toString());
    }

}
